//Usage:
// {
//   "groupid" : "",每一个演唱会的独立id
//   "personname" : "",微信名
//   "personid": "",
//   "imgurl": ""
// }

// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()

var addperson = async function (event) {
  const tencentcloud = require("tencentcloud-sdk-nodejs");

  const IaiClient = tencentcloud.iai.v20180301.Client;
  const models = tencentcloud.iai.v20180301.Models;

  const Credential = tencentcloud.common.Credential;
  const ClientProfile = tencentcloud.common.ClientProfile;
  const HttpProfile = tencentcloud.common.HttpProfile;

  let cred = new Credential("AKIDa3PWXQYgBH6lGdoMo9mIratAJNLEtuX1", "ZM0ZZedXO7Vs1iQ2d8biJ1JbpHdtaCQH");
  let httpProfile = new HttpProfile();
  httpProfile.endpoint = "iai.tencentcloudapi.com";
  let clientProfile = new ClientProfile();
  clientProfile.httpProfile = httpProfile;
  let client = new IaiClient(cred, "ap-guangzhou", clientProfile);

  let req = new models.CreatePersonRequest();



  let params = `{"GroupId":"${event.groupid}","PersonName":"${event.personname}","PersonId":"${event.personid}","Url":"${event.imgurl}"}`
  console.log(params)

  req.from_json_string(params);

  return new Promise((resolve, reject) => {
    client.CreatePerson(req, function (errMsg, response) {
      if (errMsg) {
        reject(errMsg)
      } else {
        resolve(response)
      }
    })
  })
}
// 云函数入口函数
exports.main = async (event, context) => {
  const fileList = [event.fileID]
  const result = await cloud.getTempFileURL({
    fileList,
  })
  event.imgurl = result.fileList[0].tempFileURL
  var ret = await addperson(event)
  return ret
}